#!/usr/local/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/9/18 10:38
# @Author  : Kyy
# @Site    : 
# @File    : getstockcom.py
# @Software: PyCharm
import requests
from bs4 import BeautifulSoup

class Getstockcom():
    def __init__(self,url,pageno):
        self.url = url
        self.pageno = pageno
        self.headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36'}

    def getcont(self,p):
        rst = None
        try:
            res = requests.get(self.url,params={'p':p},headers = self.headers)
            res.encoding = 'utf-8'
            if res.status_code == 200:
                rst = []
                bs = BeautifulSoup(res.text,features='lxml')
                res2 = bs.find('table','ntable').find_all('tr')
                for i in res2[1:]:
                    res3 = i.find_all('td')
                    res4 = [i.get_text(strip=True) for i in res3]
                    firm = res3[0].find('a')['href']
                    res4.append(firm)
                    rst.append(res4)
        except Exception as e :
            print("访问失败",str(e))
        return rst

    def getall(self):
        result = []
        for i in range(1,self.pageno+1):
            res = self.getcont(i)
            result.extend(res)
        return result


if __name__ == '__main__':
    test = Getstockcom(url="https://www.qichacha.com/elib_ipo.shtml", pageno=357)
    res = test.getall()
    print(res)
    print(len(res))
